import threading
import time

g_num=0


def work1():
    global g_num
    for i in range(100000):
        mutex.acquire()
        g_num+=1
        mutex.release()
    print('全局变量在work1中的值为{}'.format(g_num))#169622


def work2():
    global g_num
    for i in range(100000):
        mutex.acquire()
        g_num += 1
        mutex.release()
    print('全局变量在work2中的值为{}'.format(g_num))#200000


if __name__ == '__main__':
    mutex=threading.Lock()
    t1=threading.Thread(target=work1)
    t2=threading.Thread(target=work2)
    t1.start()
    t2.start()
    t1.join()#等子进程结束
    t2.join()
    print(g_num)#200000，保证这里是加完了200000就行，因为work1干完活的时候可能work2还没干完，只要最后是加完了就行

